package com.social.servlet;

import com.social.dao.LogDao;
import com.social.entity.Log;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
import javax.servlet.http.Part;
@WebServlet(name = "Upload_Log_Info", value = "/Upload_Log_Info")
//这是对上传文件的一些设置
@MultipartConfig(
        fileSizeThreshold = 1024 * 1024,
        maxFileSize = 1024 * 1024 * 10
)
public class Upload_Log_Info extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("add-journal.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //            获取日志公开范围


        String scope = request.getParameter("scope");
        //获取日志内容
        String content = request.getParameter("content");
//设置点赞数
        int likeNum = 0;
//提前设好id，到时候，整合我们项目时可能得用session去传
        String userId = "li";
//存储图片地址的数组
        String[] path = new String[3];
//获取当前时间
        Date date = new Date();
//因为图片地址下标是从0开始的，所以先设-1，检测到上传了图片才会+1
        int imgnum = -1;

//  不要加/，因为在部署到tomcat的项目中，我们的这个logimg正好在项目目录下，我们的jsp展示图片时用不加/的相对路径正好可以访问到，因为他们是同级目录下的
        String relativePath = "logimg";

  //设置存储在ide中项目中的实际位置，绝对路径，换电脑得改一下
        String uploadPath = "C:/Users/3444556/Desktop/Social-Media-Platform/src/main/web/"+relativePath;
        System.out.println("上传路径为"+uploadPath);

        // 根据savePath，先判断要保存的文件夹是否存在
        File saveDir = new File(uploadPath);
        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }
//        这几个就是我在add-journal.jsp设置的上传图片的input的name值

        String[] imageFields = {"img1", "img2", "img3"};

        for (String fieldName : imageFields) {
            // 检查请求中是否包含该字段
            Part filePart = request.getPart(fieldName);

            // 验证 Part 是否存在且有效（有文件名）
            if (filePart != null && filePart.getSize() > 0) {
                String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();


                // 保存文件
                String filePath = uploadPath + File.separator + fileName;
                filePart.write(filePath);
                String dbPath = relativePath + "/" + fileName;
//                把图片地址存到数组中
                imgnum+=1;
                path[imgnum] = dbPath;

            }
        }
//插入日志信息
        Log log = new Log(userId,scope,content,path,date,likeNum);
        LogDao l = new LogDao();
        int a = l.insertLog(log);


//        System.out.println(scope);
//        System.out.println(content);
    }
}